package com.example.demo.controller;

import com.example.demo.ObjectTool;
import com.example.demo.entity.AccountInfo;
import com.example.demo.entity.Auth;
import com.example.demo.entity.Equip;
import com.example.demo.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class TestController {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    private List<String> phoneNum;

    @RequestMapping("/mysql")
    public AccountInfo getDbType() {
        String sql = "SELECT * FROM account WHERE mobilenum= '18868693934'";
//        String sql = "SELECT * FROM account WHERE SUBSTR(mobilenum,1,6)= \'189571\'";

        phoneNum = new ArrayList<>();
        RowMapper<AccountInfo> rm = BeanPropertyRowMapper.newInstance(AccountInfo.class);

        AccountInfo accountInfo = jdbcTemplate.queryForObject(sql, rm);

        return accountInfo;
    }

    @RequestMapping("/equip")
    public List<Equip> getEquip() {
        String sql = "SELECT * FROM equip";
//        String sql = "SELECT * FROM account WHERE SUBSTR(mobilenum,1,6)= \'189571\'";

//        phoneNum = new ArrayList<>();
//        RowMapper<Equip> rm = BeanPropertyRowMapper.newInstance(Equip.class);

        List<Map<String, Object>> equip = jdbcTemplate.queryForList(sql);

        return ObjectTool.toObject(equip);
    }

    @RequestMapping("/addmore")
    public Result AddEquip() {
        Result result = new Result();
        String by_getLevel = "select equip_level from equip e where e.equip_id = 1";
        String sql_reduce = "update auth e set e.auth_balance = e.auth_balance - 10 where e.auth_id= 1";
        try {
            jdbcTemplate.execute(sql_reduce);
        } catch (Exception e) {
            result.setCode("-900");
            return result;
        } finally {
            int level = jdbcTemplate.queryForObject(by_getLevel, Integer.class);
            boolean succ = ObjectTool.judge(level);
            if (succ) {
                String sql = "UPDATE equip e set e.equip_level = e.equip_level + 1";
                jdbcTemplate.execute(sql);
                result.setCode("0");
            } else {
                String sql = "UPDATE equip e set e.equip_level = e.equip_level - 1 where e.equip_level > 0";
                jdbcTemplate.execute(sql);
                result.setDescription("增幅失败！！！");
                result.setCode("-1");
            }
            return result;
        }

    }

    @RequestMapping("/auth")
    public Auth getAuthInfo() {
        Auth auth = null;
        String sql = "select * from auth";
        RowMapper<Auth> rm = BeanPropertyRowMapper.newInstance(Auth.class);
        auth = jdbcTemplate.queryForObject(sql, rm);

        return auth;
    }

    @RequestMapping("/addMoney")
    public Result addMoney() {
        Result result = new Result();
        try {
            String sql = "update auth e set e.auth_balance = e.auth_balance + 100 where e.auth_id= 1";
            jdbcTemplate.execute(sql);
            result.setCode("0");
        } catch (Exception e) {
            result.setCode("1");
        } finally {
            return result;
        }

    }

//    @RequestMapping("/xyh")
//    public void show() {
//        Runtime rt = Runtime.getRuntime();
//        try {
//            Process p = rt.exec("top -c");// 调用系统的“top"命令
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//
//    @GetMapping("/test")
//    public void CupLoop() {
//        Thread.currentThread().setName("loop-thread-cpu");
//        int num = 0;
//        while (true) {
//            num++;
//            if (num == Integer.MAX_VALUE) {
//                System.out.println("reset");
//            }
//            num = 0;
//        }
//    }


}
